package com.ruoyi.lqm.controller;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.lqm.domain.LqmMaterial;
import com.ruoyi.lqm.service.AlimamaService;
import com.ruoyi.lqm.service.ILqmMaterialService;
import com.taobao.api.ApiException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * Created by DELL on 2019/9/12.
 */
@RestController
@RequestMapping("/lqm")
public class AlimamaController extends BaseController{

    @Autowired
    private AlimamaService alimamaService;
    @Autowired
    private ILqmMaterialService lqmMaterialService;

    /**
     * 物料清单API
     */
    @PostMapping("/item")
    public AjaxResult taobaoTbkDgItemCouponGet(Long pageno,String rulesId,String sortId){
        LqmMaterial lqmMaterial = new LqmMaterial();
        lqmMaterial.setRulesId(rulesId);
        lqmMaterial.setSortId(sortId);
        List<LqmMaterial> list = lqmMaterialService.selectLqmMaterialList(lqmMaterial);
        Long materialId = 13366L; //默认ID高佣榜综合
        if(list.size()>0){
            materialId = list.get(0).getMaterialId();
        }
        Object data= null;
        try {
            data = alimamaService.TbkDgOptimusMaterialRequest(pageno,materialId);
        } catch (ApiException e) {
            e.printStackTrace();
            AjaxResult.error();
        }
        return AjaxResult.success(data);
    }

    /**
     * 物料搜索API
     */
    @RequestMapping(value = "/search", method = {RequestMethod.GET,RequestMethod.POST})
    public AjaxResult TbkDgMaterialOptionalRequest(Long pageno,String q,String sort){
        Object data= null;
        pageno = pageno == null ? 1L : pageno;
        q = q == null ? "" : q;
        sort = sort == null ? "tk_rate_des" : sort;
        try {
            data = alimamaService.TbkDgMaterialOptionalRequest(pageno,q,sort);
        } catch (ApiException e) {
            e.printStackTrace();
            AjaxResult.error();
        }
        return AjaxResult.success(data);
    }
}
